﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyFinanceManager.Objects;
using MyFinanceManager.Controls;

namespace MyFinanceManager
{
    public partial class ucCategorySub : FiringUserControl
    {
        public string Value { get; private set; }
        private Repository rep;

        public ucCategorySub(Repository rep)
        {
            this.rep = rep;
            this.Value = "";
            InitializeComponent();
        }


        void item_Click(object sender, EventArgs e)
        {
            Control senderControl = sender as Control;
            if (senderControl != null)
            {
                this.Value = senderControl.Text;
                if (senderControl.Tag != null)
                    this.Value = senderControl.Tag.ToString();
            }

            FireNewValueAvailable(this.Value);
        }

        internal void Show(int catId)
        {
            List<CatInfo> ergebnis = Helper.GetSubCatInfos(catId, rep.CatInfos);

            WriteNewText(0, this.button1, ergebnis);
            WriteNewText(1, this.button2, ergebnis);
            WriteNewText(2, this.button3, ergebnis);
            WriteNewText(3, this.button4, ergebnis);
            WriteNewText(4, this.button5, ergebnis);
            WriteNewText(5, this.button6, ergebnis);
            WriteNewText(6, this.button7, ergebnis);
            WriteNewText(7, this.button8, ergebnis);
            WriteNewText(8, this.button9, ergebnis);
            WriteNewText(9, this.button10, ergebnis);
            WriteNewText(10, this.button11, ergebnis);
            WriteNewText(11, this.button12, ergebnis);

            this.Show();
        }

        private void WriteNewText(int p, Button button, List<CatInfo> ergebnis)
        {
            if (ergebnis.Count > p)
            {
                button.Text = ergebnis[p].Text;
                if (String.IsNullOrEmpty(ergebnis[p].Text))
                    button.Tag = null;
                button.Tag = ergebnis[p].Tag;
                button.Enabled = true;
            }
            else
            {
                button.Text = string.Empty;
                button.Tag = null;
                button.Enabled = false;
            }
        }
    }
}
